package smy.att;

import smy.att.devices.ISimulator;
import smy.att.devices.Qemu;
import smy.att.devices.VirtualBox;


public class Main {
	
	public static void Useage() {
		System.out.println("Android Traffic Tracker");
		System.out.println("By SMY");
		System.out.println("Capture the data traffic of specific application");
		System.out.println("Useage: Att [3G|Wifi] apk");
	}

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		if (args.length < 2) {
			Useage();
			return;
		}
		ISimulator simulator;
		String     capture_source;
		if (args[0].equalsIgnoreCase("wifi")) {
			simulator = new VirtualBox();
			capture_source = "capture_Wifi.sh";
		} else if (args[0].equalsIgnoreCase("3G")){
			simulator = new Qemu();
			capture_source = "capture_3G.sh";
		} else {
			Useage();
			return;
		}
		
		final ADB adb = ADB.Builder(simulator);
		simulator.Connect(adb);
		AndroidApplication apk = AndroidApplication.OpenApk(args[1]);
		
		adb.Unlock();
		adb.Uninstall(apk);
		adb.Install(apk);
		System.out.println("Start application");
		adb.Start(apk);
		
		adb.Push("/sdcard/capture.sh", capture_source);
		adb.Push("/sdcard/stop.sh", "stop.sh");
		System.out.println("Start Capture");
		Thread capture_thread = new Thread(new Runnable() {
			@Override
			public void run() {
				adb.Shell("sh" , "/sdcard/capture.sh");
			}
		});
		capture_thread.start();
		// Start Mointor
		System.out.println("Wait for 5 minutes");
		Thread.sleep(5 * 60 * 1000);
		adb.Shell("sh" , "/sdcard/stop.sh");
		capture_thread.join();
		adb.Pull("/sdcard/capture", apk.PackageName() + "_" + args[0] + ".pcap");
		adb.Uninstall(apk);
		System.out.println("Data is captured into file :\n"+apk.PackageName());
	}

}
